﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BLL
{
    public class KbArticleBase
    {
        /// <summary>
        /// 添加文章信息
        /// </summary>
        /// <param name="newsObj"></param>
        /// <returns></returns>
        public static string Add(Model.KbArticleBase newsObj)
        {
            return new SQLServerDAL.KbArticleBase().Add(newsObj);
        }
        /// <summary>
        /// 添加文章信息
        /// </summary>
        /// <param name="_productObj"></param>
        /// <returns></returns>
        public static string Add(Model.KbArticleBase newsObj, Model.AttachmentsBase[] _attaments)
        {
            string result = (new SQLServerDAL.KbArticleBase()).Add(newsObj);

            if (result.IndexOf("succeeded") >= 0 && (_attaments != null && _attaments.Length > 0))
            {
                string regradingObjectId = result.Split('|')[1];
                result = AttachmentsBase.AttachmentsAdmin(_attaments, regradingObjectId);

                if (result == "succeeded")
                    result += "|" + regradingObjectId;
            }

            return result;
        }
        /// <summary>
        /// 修改文章信息
        /// </summary>
        /// <param name="newsObj"></param>
        /// <returns></returns>
        public static string Update(Model.KbArticleBase newsObj)
        {
            return new SQLServerDAL.KbArticleBase().Update(newsObj);
        }        
        /// <summary>
        /// 修改文章信息
        /// </summary>
        /// <param name="newsObj"></param>
        /// <returns></returns>
        public static string Update(Model.KbArticleBase newsObj, Model.AttachmentsBase[] _attaments)
        {
            string result = (new SQLServerDAL.KbArticleBase()).Update(newsObj);

            if (result.IndexOf("succeeded") >= 0)
            {
                string regradingObjectId = result.Split('|')[1];

                if (_attaments != null && _attaments.Length > 0)
                    AttachmentsBase.AttachmentsAdmin(_attaments, regradingObjectId);
                else
                    AttachmentsBase.DeleteAttachmentsByRegardingObjectId(regradingObjectId);
            }

            return result;
        }
        public static void Delete(string articleid)
        {
            new SQLServerDAL.KbArticleBase().Delete(articleid);
        }
        /// <summary>
        /// 匹配 Id 获取文章对象
        /// </summary>
        /// <param name="articleid"></param>
        /// <returns></returns>
        public static Model.KbArticleBase GetObjById(string articleid)
        {
            if (Common.Utils.IsGuid(articleid))
                return (new SQLServerDAL.KbArticleBase()).GetObjById(articleid);
            else
                return null;
        }
        /// <summary>
        /// 获取数据表中最大的编号
        /// </summary>
        /// <returns></returns>
        public static string GetArticleNumber()
        {
            string temp = "";

            object _obj = BLL.CommonBLL.ExecuteScalar("KbArticleBase", "Top 1 ArticleNumber", " ORDER BY ArticleNumber DESC");
            if (_obj != null)
            {
                temp = _obj.ToString();
                if (temp == "")
                    temp = "0";
            }
            else
                temp = "0";

            int _number = int.Parse(temp) + 1;

            return _number.ToString();
        }
        /// <summary>
        /// 获取数据表中最大的编号
        /// </summary>
        /// <returns></returns>
        public static string GetArticleNumber(string c_number)
        {
            string temp = "";

            object _obj = BLL.CommonBLL.ExecuteScalar("vw_KbArticleBaseForList", "Top 1 ArticleNumber", " AND CategoryNumber LIKE '" + c_number + "%' ORDER BY ArticleNumber DESC");
            if (_obj != null)
            {
                temp = _obj.ToString();
                if (temp == "")
                    temp = "0";
            }
            else
                temp = "0";

            int _number = int.Parse(temp) + 1;

            return _number.ToString();
        }

        /// <summary>
        /// 更新文章点击次数
        /// </summary>
        public static void UpdateHits(string articleid)
        {
            (new SQLServerDAL.KbArticleBase()).UpdateHits(articleid);
        } 
    }
}
